{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle as pkl\n",
    "import scipy\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from Dataset4LAL import DatasetSimulated\n",
    "from Tree4LAL import Tree4LAL\n",
    "from LALmodel import LALmodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "experiment = dict()\n",
    "# number of datasets for which we will generate data\n",
    "experiment['n_datasets'] = 500\n",
    "# how many datapoints will be labelled at the beginning, including 1 positive and 1 negative\n",
    "experiment['n_labelleds'] = np.arange(2,50,1)\n",
    "# how many times we will sample data with the same parameters\n",
    "experiment['n_points_per_experiment'] = 10\n",
    "# dimensionality of the data\n",
    "experiment['n_dim'] = 2\n",
    "# measure of quality change\n",
    "experiment['method'] = 'error'\n",
    "# for now 2 techniques for tree growing are available, random that means just adding random samples and iterative for adding points based on previously build model\n",
    "experiment['treegrowing'] = 'random'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "n_lablled =  2\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  3\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  4\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  5\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  6\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  7\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  8\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  9\n",
      "********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n",
      "n_lablled =  10\n",
      "*****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"
     ]
    }
   ],
   "source": [
    "np.random.seed(805)\n",
    "\n",
    "nDatapoints = 400\n",
    "lalModels = []\n",
    "\n",
    "all_data_for_lal = np.array([[]])\n",
    "all_labels_for_lal = np.array([[]])\n",
    "\n",
    "all_sizes_data_for_lal = np.array([[]])\n",
    "all_sizes_labels_for_lal = np.array([[]])\n",
    "\n",
    "\n",
    "for n_labelled in experiment['n_labelleds']:\n",
    "    \n",
    "    print()\n",
    "    print('n_lablled = ', n_labelled)\n",
    "\n",
    "    all_data_for_lal = np.array([[]])\n",
    "    all_labels_for_lal = np.array([[]])\n",
    "    for i_dataset in range(experiment['n_datasets']):\n",
    "        print('*', end='')\n",
    "        dataset = DatasetSimulated(nDatapoints, experiment['n_dim'])\n",
    "        tree = Tree4LAL(experiment['treegrowing'], dataset, lalModels, experiment['method'])\n",
    "        tree.generateTree(n_labelled)\n",
    "        data_for_lal, labels_for_lal = tree.getLALdatapoints(experiment['n_points_per_experiment'])\n",
    "\n",
    "        # stack LAL data together\n",
    "        if np.size(all_data_for_lal)==0:\n",
    "            all_data_for_lal = data_for_lal\n",
    "            all_labels_for_lal = labels_for_lal\n",
    "        else:\n",
    "            all_data_for_lal = np.concatenate((all_data_for_lal, data_for_lal), axis=0)\n",
    "            all_labels_for_lal = np.concatenate((all_labels_for_lal, labels_for_lal), axis=0)\n",
    "\n",
    "    \n",
    "    if experiment['treegrowing']=='iterative':\n",
    "        # for every size of the tree train a lal model and attach it to the list of models for all sizes of trees\n",
    "        # also let's do some cross validation to find better parameters \n",
    "        lalModel = LALmodel(all_data_for_lal, all_labels_for_lal)\n",
    "        lalModel.crossValidateLALmodel()\n",
    "        lalModels.append(lalModel.model)\n",
    "    \n",
    "    # data to save to build the big tree at the end\n",
    "    \n",
    "    if np.size(all_sizes_data_for_lal)==0:\n",
    "        all_sizes_data_for_lal = all_data_for_lal\n",
    "        all_sizes_labels_for_lal = all_labels_for_lal\n",
    "    else:\n",
    "        all_sizes_data_for_lal = np.concatenate((all_sizes_data_for_lal, all_data_for_lal), axis=0)\n",
    "        all_sizes_labels_for_lal = np.concatenate((all_sizes_labels_for_lal, all_labels_for_lal), axis=0)\n",
    "    np.savez('./lal datasets/LAL-randomtree-simulated2Gauss2dim', all_sizes_data_for_lal, all_sizes_labels_for_lal)\n",
    "    \n",
    "lalModel = LALmodel(all_sizes_data_for_lal, all_sizes_labels_for_lal)\n",
    "lalModel.crossValidateLALmodel()\n",
    "\n",
    "print(all_sizes_data_for_lal.shape)\n",
    "print(all_sizes_labels_for_lal.shape)\n",
    "\n",
    "np.savez('./lal datasets/LAL-randomtree-simulated2Gauss2dim', all_sizes_data_for_lal, all_sizes_labels_for_lal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
